package com.muyu.blog.admin.service;

import com.muyu.blog.admin.dao.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Component
@Slf4j
public class SecurityUserService implements UserDetailsService {

    @Autowired
    private AdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("username{}",username);

        Admin adminUser = adminService.findAdminByUserName(username);
        if(adminUser==null){
            throw new UsernameNotFoundException("用户名不存在");
        }

        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        User userDetails = new User(username, adminUser.getPassword(), authorities);
        return userDetails;
    }


}
